var loadPageList = [];
var scrollStatus = true;
/**
 * 监听页面滚动
 */
function listenerScroll() {
    var scrollIndex=0;
    $(window).scroll(function(){
        if(scrollStatus==true){
            //只获取不是第一个，且没有加载过试题内容元素
            var list = $("#question-list-box>div[data-no]").filter(function (index) {
                return $(this).data('no') != 1 && $(this).children().length ==0;
            });
            //页面可视区的高度
            var winHeight = $(window).height();
            //可视区距离顶部的高度
            var scrollTopHeight = $(window).scrollTop();

            if(list.length > 0){
                var em = null;
                if(scrollTopHeight <= scrollIndex){//向上
                    em = list[list.length -1];
                }else{//向下
                    em = list[0];
                }
                em = $(em);
                //元素距离顶部的高度
                var emTopHeight = em.offset().top;
                //元素外部高度
                var outerHeight = em.outerHeight(true);
                //如果元素在可视区域内
                if(!(scrollTopHeight >(emTopHeight+outerHeight) || (winHeight+ scrollTopHeight)<  emTopHeight)){
                    var pageNo = em.data('no');
                    var children = em.children();
                    if(children.length <= 0){
                        loadPage(pageNo);
                    }
                }
            }
            setTimeout(function () {
                scrollIndex = scrollTopHeight;
            },30);
        }
    });
}

/**
 * 加载页面数据
 * @param _pageNo
 */
function loadPage(_pageNo,callback) {
    var examRecordId = $("#examRecordId").val();
    var userId = $("#userId").val();
    var type = $("#type").val();
    var status = $("#status").val();
    var marking = $("#marking").val();
    var marked = $("#marked").val();
    loadQuestionList({
        userId:userId,
        examRecordId:examRecordId,
        type:type,
        status:status,
        marking:marking,
        marked:marked,
        pageNo:_pageNo,
        callback:callback
    });
}

/**
 * 加载试题列表
 * @param obj
 */
function loadQuestionList(obj) {
    if(loadPageList.indexOf(obj.pageNo) ==-1){
        loadPageList.push(obj.pageNo);
        $.ajax({
            url: baseloaction +'/admin/exam/record/loadQuestionList',
            type:'post',
            dataType:'html',
            data:{
                'type':obj.type,
                'status':obj.status,
                'examRecordId':obj.examRecordId,
                'userId':obj.userId,
                'marking':obj.marking,
                'marked':obj.marked,
                'currentPage':obj.pageNo
            },
            success:function (html) {
                if(obj.pageNo==1){
                    $("#question-list-box").append(html);
                }else{
                    var pageEm = $("#question-list-box div[data-no='"+obj.pageNo+"']");
                    $(html).insertBefore($(pageEm));
                    $("#question-list-box .temporary-box[data-no='"+obj.pageNo+"']").remove();
                }
                if($.isFunction(obj.callback)){
                    obj.callback();
                }
            },
            error:function (error) {
                icModale(0,'系统错误，试题列表加载失败');
            }
        });
    }else{
        if($.isFunction(obj.callback)){
            obj.callback();
        }
    }
}

/**
 * 加载答题卡
 * @param userId 用户ID
 * @param examRecordId 考试记录ID
 */
function loadCard(userId,examRecordId){
    $.ajax({
        url: baseloaction +'/admin/exam/record/loadExamCard',
        type:'post',
        dataType:'html',
        data:{
            'userId':userId,
            'examRecordId':examRecordId
        },
        success:function (html) {
            $("#card-list-box").html(html);
            //固定答题卡栏
            $('.tp-marking-rb-wrap').slimScroll({
                height: '100%',
                railOpacity: 0.9,
                alwaysVisible: false
            });
            //答题卡定位
            tpmFun();
            $("#card-list-box a[data-no]").click(function () {
                scrollStatus = false;
                var pageNo = $(this).data('no');
                var questionRecordId = $(this).data('id');
                loadPage(pageNo,function () {
                    $('body,html').animate({
                        scrollTop: $('#target_'+questionRecordId).offset().top
                    },500,"swing",function () {
                        scrollStatus = true;
                    });
                });
            });
        },
        error:function (error) {
            icModale(0,'系统错误，加载答题卡失败');
        }
    });
}

//答题卡定位
function tpmFun() {
    var _tpm = $('.tp-marking-right-bx'),
        _tm = function () {
            var _stop = parseInt((document.documentElement.scrollTop || document.body.scrollTop), 10);
            if (_stop > 60) {
                _tpm.css({'position': 'fixed', 'top': 0})
            } else {
                _tpm.css({'position': 'absolute', 'top': 60 + 'px'})
            }
        };
    $(window).bind('scroll', _tm)
}